package br.edu.sistemaacademico.builder;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.pojo.Aluno;
import br.edu.sistemaacademico.pojo.Disciplina;

public class DisciplinaDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"codigo", "nome", "requisito", "cargaHoraria",  "status" });

	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	
	@Override
	public void alterar(Object o) throws SQLException {
		Disciplina d = (Disciplina) o;
		List<Object> valores = Arrays.asList(new Object[] { d.getId(),
				d.getNome(), d.getRequisito().getId(), d.getCargaHoraria(), d.getStatus() });
		if(d.getRequisito().getId()==0){
			valores = Arrays.asList(new Object[] { d.getId(),
					d.getNome(), "Null", d.getCargaHoraria(), d.getStatus() });
		}
		String condicao = "codigo = " + d.getId()+" and status =1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_DISCIPLINA");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<Object> getAll(String condicao, String tabelasAdicionais, String ordenacao){
		Disciplina d = new Disciplina();
		List<Disciplina> list = new LinkedList<Disciplina>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Disciplina a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.colunasToReturn = "a.*";
		select.setCondicao(condicao+" and status =1");
		select.orderBy = ordenacao;
		dir.buildSQL(select);
		System.out.println(select.getSQL());
		try {
			ResultSet rs = executeQuery(select.getSQL());
			while (rs.next())
				list.add( (Disciplina) informacoes(rs));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Disciplina d = new Disciplina();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Disciplina a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao+" and status =1");
		select.colunasToReturn = "a.*";

		dir.buildSQL(select);
		System.out.println(select.getSQL());
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				d = (Disciplina) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return d;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		Disciplina d = (Disciplina) o;
		List<Object> valores;
		if(d.getRequisito()==null){
			valores = Arrays.asList(new Object[] { d.getId(),
					d.getNome(), "Null", d.getCargaHoraria(),1 });
		}else{
			valores = Arrays.asList(new Object[] { d.getId(),
				d.getNome(), d.getRequisito().getId(), d.getCargaHoraria(),1 });
		}
		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_Disciplina");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);
		
		dir.buildSQL(insert);
		try {
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		Disciplina a = new Disciplina();
		Long id;
		a.setId(rs.getLong(COLUNAS.get(0)));
		a.setNome(rs.getString(COLUNAS.get(1)));
		try{
			Disciplina requisito = new Disciplina();
			a.setRequisito(requisito);
			id = rs.getLong(COLUNAS.get(2));
			if(id==null)
				a.getRequisito().setId(0l);
			else
		a.getRequisito().setId(rs.getLong(COLUNAS.get(2)));
		}catch(NullPointerException e){
			a.getRequisito().setId(0L);
		}
		a.setCargaHoraria(rs.getInt(COLUNAS.get(3)));
		a.setStatus(rs.getInt(COLUNAS.get(4)));
		return a;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Disciplina d = (Disciplina) o;
		List<Object> valores = Arrays.asList(new Object[] { d.getId(),
				d.getNome(), d.getRequisito(), d.getCargaHoraria() });
		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_Disciplina");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		dir.buildSQL(delete);
		try {
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	public void deletar(Object o) {
		Disciplina d = (Disciplina) o;
		List<Object> valores = Arrays.asList(new Object[] { d.getId(),
				d.getNome(), d.getRequisito().getId(), d.getCargaHoraria(), 0 });

		String condicao = "codigo = " + d.getId()+" and status =1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_DISCIPLINA");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		System.out.println(update.getSQL());
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
